import re, os, datetime, json, pprint
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

def write_data(item):
    source_dir = 'video_data'
    target_dir = os.path.join(source_dir, item['标题']) # 将标题作为文件夹名称存入video_data内
    if not os.path.exists(target_dir):
        os.mkdir(target_dir)
    file_name = "{}.txt".format(datetime.datetime.now().strftime("%Y_%m_%d")) #文件名称格式
    file_path = os.path.join(target_dir, file_name)   #将文件存入对应的文件夹
    with open(file_path, mode='a', encoding='utf8') as file:  #创建文件
        file.write(json.dumps(item, indent=4, ensure_ascii=False))    #将网页数据的json格式转换为字典
        file.write('\n')


def main():
    chrome_options =  Options()
    prefs = {"profile.managed_default_content_settings.images": 2}
    chrome_options.add_experimental_option('prefs', prefs)
    chrome_options.add_argument('--headless')
    chrome_options.add_argument("--window-size=3840，2160")
    driver = webdriver.Chrome(chrome_options=chrome_options)
    driver.get('https://www.bilibili.com')
    element = driver.find_elements(By.XPATH, ".//div[@class='feed-card']")

    for i in element:
        item = {}
        item['标题'] = i.find_element(By.XPATH,'.//div/div[2]/div/div[2]/div/h3[@class="bili-video-card__info--tit"]').get_attribute('title')
        item['作者'] = i.find_element(By.XPATH,'.//div/div[2]/div/div[2]/div/div[2]/a/span[1]').text
        item['日期'] = i.find_element(By.XPATH, './/div/div[2]/div/div[2]/div/div[2]/a/span[2]').text
        item['url'] = i.find_element(By.XPATH, './/div/div[2]/div/a').get_attribute('href')
        item['播放量'] = i.find_element(By.XPATH, './/div/div[2]/div/a/div/div[2]/div/div/span[1]/span').text
        item['弹幕数量'] = i.find_element(By.XPATH, './/div/div[2]/div/a/div/div[2]/div/div/span[2]/span').text
        item['时间'] = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
        write_data(item)
        pprint.pprint(item)
        sleep(1)

    driver.quit()

if __name__ == "__main__":
    main()